﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SpaceHero.Screens;

namespace SpaceHero
{
    public enum States
    { 
        ground,
        air
    }
    class Player
    {
        public Vector2 position;
        public Vector2 followPosition;

        Vector2 speed;
        Vector2 direction;

        States state;
        Texture2D texture;

        public Player(Vector2 pos, int [,] world)
        {
            position = pos;

            speed = Vector2.Zero;
            direction = Vector2.Zero;
            state = States.ground;
        }
        
        public void LoadContent(ContentManager Content)
        {
            texture = Content.Load<Texture2D>("blank");
        }

        public void Update(GameTime gameTime, KeyboardState keyState, KeyboardState oldKeyState, int[,] world)
        {
            UpdateMovement(keyState);
            position += direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            //Make FollowCamera Follow Player
            followPosition = position;
        }

        public void Draw(GraphicsDeviceManager graphics, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, rectangle(), Color.White);
        }

        public Rectangle rectangle()
        {
            return new Rectangle((int)position.X - 16, (int)position.Y - 16, 32, 32);
        }

        private void UpdateMovement(KeyboardState keyState)
        {
            if (state == States.ground)
            {
                speed = Vector2.Zero;
                direction = Vector2.Zero;

                if (keyState.IsKeyDown(Keys.Left))
                {
                    speed.X = 200;
                    direction.X = -1;
                }
                else if (keyState.IsKeyDown(Keys.Right))
                {
                    speed.X = 200;
                    direction.X = 1;
                }

                if (keyState.IsKeyDown(Keys.Up))
                {
                    speed.Y = 200;
                    direction.Y = -1;
                }
                else if (keyState.IsKeyDown(Keys.Down))
                {
                    speed.Y = 200;
                    direction.Y = 1;
                }
            }
        }

        private void UpdateJump(KeyboardState keyState, KeyboardState oldKeyState)
        {
            if (state == States.ground) 
            {
                if (keyState.IsKeyDown(Keys.Space) && oldKeyState.IsKeyUp(Keys.Space))
                {
                    Jump();
                }
            }

            if (state == States.air)
            {
            }
        }

        private void Jump()
        { 
            
        }
    }
}
